package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.itboye.lottery.R;
import com.itboye.lottery.enums.LotteryType;
import com.itboye.lottery.utils.OnItemClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class NoticeDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private JSONArray jsonArrayColorBall;
    private LotteryType lottery;
    private Activity context;
    public Object mData;
    private LayoutInflater inflater;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public NoticeDetailAdapter(Activity context, Object mData, LotteryType lotteryType) {
        this.context = context;
        this.mData = mData;
        this.inflater = LayoutInflater.from(context);
        this.lottery = lotteryType;
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        String jsonString = gson.toJson(mData);
        try {
            switch (lotteryType) {
                case SSQ:
                    jsonArrayColorBall = new JSONArray(jsonString);
                    break;
                case DLT:
                    jsonArrayColorBall = new JSONArray();
                    JSONObject jsonObject = new JSONObject(jsonString);
                    Iterator<String> iterator = jsonObject.keys();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        JSONObject jsonObChild = jsonObject.getJSONObject(key);
                        JSONArray jsonArrayInner = new JSONArray();
                        jsonArrayInner.put(key);
                        jsonArrayInner.put(jsonObChild.getJSONArray("j").get(0) + "");
                        jsonArrayInner.put(jsonObChild.getJSONArray("j").get(1) + "");
                        jsonArrayColorBall.put(jsonArrayInner);
                    }
                    break;
                case JCLQ:
                    break;
                case JCZQ:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_notice_detail, parent, false);
        NoticeDetailAdapter.NoticeDetailViewHolder holder = new NoticeDetailAdapter.NoticeDetailViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NoticeDetailAdapter.NoticeDetailViewHolder viewHolder = (NoticeDetailAdapter.NoticeDetailViewHolder) holder;
        if (position != 0) {
            try {
                viewHolder.item_notice_detail_1.setText(jsonArrayColorBall.getJSONArray(position - 1).get(0) + "等奖");
                viewHolder.item_notice_detail_2.setText(jsonArrayColorBall.getJSONArray(position - 1).get(1) + "");
                viewHolder.item_notice_detail_3.setText(jsonArrayColorBall.getJSONArray(position - 1).get(2) + "");
                viewHolder.item_notice_detail_3.setTextColor(context.getResources().getColor(R.color.red));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (position % 2 != 0) {
                viewHolder.item_notice_detail_1.setBackgroundColor(context.getResources().getColor(R.color.gray_f8));
                viewHolder.item_notice_detail_2.setBackgroundColor(context.getResources().getColor(R.color.gray_f8));
                viewHolder.item_notice_detail_3.setBackgroundColor(context.getResources().getColor(R.color.gray_f8));
            } else {
                viewHolder.item_notice_detail_1.setBackgroundColor(context.getResources().getColor(R.color.white));
                viewHolder.item_notice_detail_2.setBackgroundColor(context.getResources().getColor(R.color.white));
                viewHolder.item_notice_detail_3.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        } else {
            viewHolder.item_notice_detail_3.setTextColor(context.getResources().getColor(R.color.black));
        }
    }

    @Override
    public int getItemCount() {
        return jsonArrayColorBall == null ? 0 : jsonArrayColorBall.length() + 1;
    }

    public class NoticeDetailViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        Button item_notice_detail_1, item_notice_detail_2, item_notice_detail_3;

        public NoticeDetailViewHolder(View itemView) {
            super(itemView);
            item_notice_detail_1 = itemView.findViewById(R.id.item_notice_detail_1);
            item_notice_detail_2 = itemView.findViewById(R.id.item_notice_detail_2);

            item_notice_detail_3 = itemView.findViewById(R.id.item_notice_detail_3);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }
}
